function [ghandle leghandle]=plot_multiple3d(mat,s)
% =========================================================================
% function ghandle=plot_multiple3d(mat,s)
% Plot 3d matrices from different models 
%
% Inputs
% MAT [nr nc np nb]  
% Matrix where each of the NB might correspond to a different model 
% Code will plot each of the NB [1 1 NP] matrices for each row and column 
% Use this for IRFD and VDECS perturbations or models 
% 
% Structure S contains some required, other optional arguments 
% Required
% s.bnames      Cell names of books             
%               This will be used to create legends 
% s.rnames      Cell names of rows
% s.cnames      Cell names of columns 
% 
% Optional 
% s.xaxis       [1 np] vector with the axis (optional) 
%               default is (1:NP) 
% s.titfont     Default is 9
% s.suptitle    Default is none 
% s.suptitfont  Default is 11 if suptitle provided 
% s.width       See below 
% s.style       See below 
% s.markwidth   See below 
% s.xtick       xaxis(1):np/4:xaxis(end); 
% s.legfont     7 
% s.subdesign       [nr nc]  Number of rows and columns 
%                   If choose design with an extra subplot 
%                   Legend will be plotted there 
% s.flag_noxtick    Default is =1, will only plot last row 
% s.flag_nolegend   Default is =0. 
% s.axesfontsize    Size of fonts for graphs 
% Alejandro Justiniano September 28 2009 
% Last Modified May 18 2010 
% =========================================================================
[nr nc np nb]=size(mat); 
if length(s.bnames)~=nb;error('S.BNAMES does not much number of books');end 
if length(s.cnames)~=nc;error('S.CNAMES does not much number of columns');end 
if length(s.rnames)~=nr;error('S.RNAMES does not much number of rows');end 
if nb > 7;error('Code can only handle up to 7 different lines');end
% =========================================================================
% Defaults 
% =========================================================================
% SUPTITLE (none) and SUPTITFONT (11)
[flag_suptitle,s]=ch_field(s,'suptitle');
if flag_suptitle==1; 
    [e,s]=ch_field(s,'suptitfont',11);
end 
% XAXIS (1:NP)
[e, s] = ch_field(s, 'add_hline', 0);
add_hline = s.add_hline; 

[e, s] = ch_field(s, 'boxon', 1);
boxon = s.boxon; 


[e,s]=ch_field(s,'xaxis',(1:np) ); 
if e==1;if length(s.xaxis)~=np;error('S.XAXIS does not match number of pages');end;end
% TITFONT (9)
[e,s]=ch_field(s,'titfont',9); 
% WIDTH, STYLE and MARKWIDTH 
style    ={'k','go-','bx-','r--','cd--','k^:','gh-'}; 
width    =[2    1       1    2      1.5  2      2]; 
markwidth=[1    7       7    5      5    7      7];
[e,s]=ch_field(s,'width'); 
if e==0;s.width=width;end 
[e,s]=ch_field(s,'style'); 
if e==0;s.style=style;end 
[e,s]=ch_field(s,'markwidth'); 
if e==0;s.markwidth=markwidth;end 
% XTICK (1 through END divided by 4) 
flag_stick=1; 
[e,s]=ch_field(s,'xtick');if e==0; flag_stick=0; end; %s.xtick=round((s.xaxis(1):(np/5):s.xaxis(np)));end 
% LEGFONT 
[e,s]=ch_field(s,'legfont',7); 
% SUBDESIGN 
[e,s]=ch_field(s,'subdesign',[nr nc]); 
if s.subdesign(1)*s.subdesign(2) < nr*nc; 
    error('SUBDESIGN does not cover the size of the matrix') 
end 
[e,s]=ch_field(s,'flag_noxtick',1); 
[e,s]=ch_field(s,'flag_nolegend',0); 
[e,s]=ch_field(s,'axesfontsize' ,7); 

set(0,'DefaultAxesFontSize',s.axesfontsize); 
if s.subdesign(1)*s.subdesign(2) > nr*nc; 
    flag_extra=1;
else 
    flag_extra=0; 
end 

% ========================================================================
ghandle=figure; 
gind=0; 
for ii=1:nr;
    for jj=1:nc;
        gind=gind+1;
        subplot(s.subdesign(1),s.subdesign(2),gind);
        templim=[1e10 -1e10];
        for kk=1:nb
            temp=squeeze(mat(ii,jj,:,kk));
            templim(1)=min( min(temp),templim(1) );
            templim(2)=max( max(temp),templim(2) );
            hand=plot(s.xaxis(:),temp,s.style{kk},'LineWidth',s.width(kk));
            set(hand,'MarkerSize',s.markwidth(kk))
            if boxon == 0; 
                box off
            end 
            if add_hline == 1; 
                hline(0, 'b:'); 
            end 
            if kk==1;hold on;end
        end
        tempoffset=max(abs(templim(2)-templim(1))/8,0.02);  
        ylim([templim(1)-tempoffset templim(2)+tempoffset]); 
        xlim([s.xaxis(1)      s.xaxis(end)   ]); 
        title([s.rnames{ii},'  ',s.cnames{jj}],'FontSize',s.titfont);
    end
    if jj < nc && flag_noxtick==1
        set(gca,'XTickLabel','')
    else
        if flag_stick==1
            set(gca,'Xtick',s.xtick);
        end
    end
    % Suptitle called before legend 
    if ii==nr && jj==nc && flag_extra==0 
        if flag_suptitle==1; 
            suptitle(s.suptitle,s.suptitfont); 
        end     
        if s.flag_nolegend==0
            % hand=legend(s.bnames); % ,'Location','SouthOutside');
            hand=legend(s.bnames,'Location','SouthEast');
            set(hand,'box','off');
            set(hand,'FontSize',s.legfont,'FontWeight','Bold')
        end 
    end   
end     
if flag_extra==1; 
    subplot(s.subdesign(1),s.subdesign(2),gind+1); 
    for kk=1:nb
        hand=plot(s.xaxis(:),zeros(np,1),s.style{kk},'LineWidth',s.width(kk));
        set(hand,'MarkerSize',markwidth(kk))
        if kk==1;hold on;end
    end
    ylim([-1000 1000]); 
    
    if flag_suptitle==1;
        suptitle(s.suptitle,s.suptitfont);
    end
    
    hand=legend(s.bnames,'Location','NorthWest');
    set(hand,'box','off');
    set(hand,'FontSize',s.legfont,'FontWeight','Bold');
end 
set(ghandle,'PaperPosition',[0.75 0.5 7.5 10])
set(0,'DefaultAxesFontSize','remove'); 
leghandle = hand;
